package com.lb.shopguide.adapter;

import android.support.annotation.Nullable;
import android.text.TextUtils;
import android.view.View;
import android.widget.ImageView;
import android.widget.LinearLayout;
import android.widget.TextView;

import com.blankj.utilcode.util.TimeUtils;
import com.chad.library.adapter.base.BaseQuickAdapter;
import com.chad.library.adapter.base.BaseViewHolder;
import com.lb.shopguide.R;
import com.lb.shopguide.constants.AppConstant;
import com.lb.shopguide.entity.recommend.RecommendGoodsBean;
import com.lb.shopguide.ui.view.LbCountDownWidgetInList;
import com.lb.shopguide.util.pic.ImageLoaderUtils;

import java.util.Calendar;
import java.util.Date;
import java.util.List;

/**
 * Create by kangzhao on 2018/11/18
 */
public class AdapterRecommendGoods extends BaseQuickAdapter<RecommendGoodsBean, BaseViewHolder> {
    public AdapterRecommendGoods(int layoutResId, @Nullable List<RecommendGoodsBean> data) {
        super(layoutResId, data);
    }

    @Override
    protected void convert(BaseViewHolder helper, RecommendGoodsBean item) {
        View view = helper.itemView;
        ImageView ivGoodsImage = view.findViewById(R.id.iv_goods_image);
        TextView tvGoodsName = view.findViewById(R.id.tv_goods_name);
        TextView tvGoodsPrice = view.findViewById(R.id.tv_goods_price);
        TextView tvShare = view.findViewById(R.id.tv_share);
        TextView tvNoEarning = view.findViewById(R.id.tv_no_earnings);
        LinearLayout layoutEarnMoney = view.findViewById(R.id.layout_earn_money);
        TextView tvEarnPrice = view.findViewById(R.id.tv_earn_price);
        View ivVerLine = view.findViewById(R.id.iv_ver_line);
        LbCountDownWidgetInList countDownWidget = view.findViewById(R.id.lb_count_down_widget);
        TextView tvTimeUntilStart = view.findViewById(R.id.tv_time_until_start);
        TextView tvSaleOut = view.findViewById(R.id.tv_sale_out);

        ImageLoaderUtils.display(mContext, ivGoodsImage, item.getProductPicUrl());
        tvGoodsName.setText(item.getProductName());
        tvGoodsPrice.setText("￥" + item.getSalePrice());
        countDownWidget.setVisibility(View.GONE);
        tvTimeUntilStart.setVisibility(View.GONE);
        tvShare.setText("分享");
        tvEarnPrice.setVisibility(View.VISIBLE);
        ivVerLine.setVisibility(View.VISIBLE);
        layoutEarnMoney.setVisibility(View.GONE);
        tvTimeUntilStart.setVisibility(View.GONE);
        countDownWidget.setVisibility(View.GONE);
        tvTimeUntilStart.setVisibility(View.GONE);
        if (item.getTotalStoreSum() == 0) {
            tvSaleOut.setVisibility(View.VISIBLE);
            tvShare.setVisibility(View.GONE);
            if (!item.isSecSymbol()) {
                layoutEarnMoney.setVisibility(View.VISIBLE);
                ivVerLine.setVisibility(View.VISIBLE);
                tvEarnPrice.setText("￥" + item.getStaffBen());
            }
        } else {
            tvSaleOut.setVisibility(View.GONE);
            tvShare.setVisibility(View.VISIBLE);
            if (item.isSecSymbol()) {//秒杀
                tvNoEarning.setVisibility(View.VISIBLE);
                tvEarnPrice.setVisibility(View.GONE);
                ivVerLine.setVisibility(View.GONE);
                if (item.getSecStatus() == AppConstant.GOODS_SEC_START) {
                    countDownWidget.setVisibility(View.VISIBLE);
                    countDownWidget.setTime(item.getSecEndTimeStamp() - System.currentTimeMillis());
                } else if (item.getSecStatus() == AppConstant.GOODS_SEC_NOT_START) {
                    tvTimeUntilStart.setVisibility(View.VISIBLE);
                    Date date = TimeUtils.millis2Date(item.getSecBeginTimeStamp());
                    Calendar cal = Calendar.getInstance();
                    cal.setTime(date);

                    if (TimeUtils.isToday(date)) {
                        tvTimeUntilStart.setText("今天 " + cal.get(Calendar.HOUR_OF_DAY) + ":" + cal.get(Calendar.MINUTE) + "开始");
                    } else {
                        tvTimeUntilStart.setText(cal.get(Calendar.MONTH) + 1 + "-" + cal.get(Calendar.DAY_OF_MONTH) + " " + cal.get(Calendar.HOUR_OF_DAY) + ":" + cal.get(Calendar.MINUTE) + "开始");
                    }
                    tvShare.setText("先分享");
                } else if (item.getSecStatus() == AppConstant.GOODS_SEC_END) {
                    countDownWidget.setVisibility(View.GONE);
                    tvTimeUntilStart.setVisibility(View.GONE);
                }
            } else {
                tvNoEarning.setVisibility(View.GONE);
                if (!TextUtils.isEmpty(item.getSalePrice())) {
                    layoutEarnMoney.setVisibility(View.VISIBLE);
                    ivVerLine.setVisibility(View.VISIBLE);
                    tvEarnPrice.setText("￥" + item.getStaffBen());
                } else {
                    ivVerLine.setVisibility(View.GONE);
                }
            }
        }

        tvShare.setOnClickListener(new View.OnClickListener() {
            @Override
            public void onClick(View v) {
                if (mOnRecommendGoodsListener != null) {
                    mOnRecommendGoodsListener.onShareClick(item);
                }
            }
        });
        countDownWidget.setOnTimeOverListener(new LbCountDownWidgetInList.OnTimeOverListener() {
            @Override
            public void onTimeOver() {
                if (mOnRecommendGoodsListener != null) {
                    mOnRecommendGoodsListener.onCountDownOver(item);
                }
            }
        });
    }


    public interface OnRecommendGoodsListener {
        void onShareClick(RecommendGoodsBean bean);

        void onCountDownOver(RecommendGoodsBean bean);
    }

    private OnRecommendGoodsListener mOnRecommendGoodsListener;

    public void setOnRecommendGoodsListener(OnRecommendGoodsListener listener) {
        mOnRecommendGoodsListener = listener;
    }
}
